package com.hourses.service.impl;

import com.hourses.mapper.InStoreMapper;
import com.hourses.page.Page;
import com.hourses.pojo.InStore;
import com.hourses.pojo.Result;
import com.hourses.service.InstoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/9 23:50
 */
@Service
public class InstoreServiceImpl implements InstoreService {

    //分页展示入库单
    @Autowired
    private InStoreMapper inStoreMapper;
    @Override
    public Result selectInstorePageList(InStore inStore, Page page) {
        //step1:统计满足条件入库单总数
        Integer count = inStoreMapper.selectTotallNum(inStore);
        page.setTotalNum(count);
        //step2:查询满足条件的每一页要展示的入库单集合
        List<InStore> inStores = inStoreMapper.selectPageList(inStore, page);
        page.setResultList(inStores);
        return Result.ok(page);
    }

    //最终的入库操作
    //step1:先把in_store表中的is_in字段由0改为1，同时将create_time字段更新为当前时间
    //step2:最后修改商品库存数量
    @Override
    @Transactional    //添加事务
    public Result endInStore(InStore inStore) {
        //step1:修改in_store表中的is_in字段和create_time字段
        Integer count = inStoreMapper.updateInStoreIsInAndCteateTime(inStore);
        if (count>0){
            //is_in字段修改成功,再修改商品库存数量
            Integer count1 = inStoreMapper.updateProductInvent(inStore);
            if (count1>0) {
                //商品库存修改成功,才代表最终入库成功
                return Result.ok("入库成功!商品库存更新成功！");
            }
            //如果商品库存修改失败，入库也不成功
            return Result.err(Result.CODE_ERR_BUSINESS,"入库失败！请联系管理员...");
        }
        //is_in字段修改失败
        return Result.err(Result.CODE_ERR_BUSINESS,"入库失败！请联系管理员...");
    }
}
